package com.ruoyi.machine.base.wearing.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.machine.base.wearing.domain.WearingPart;
import com.ruoyi.machine.base.wearing.domain.WearingPartCond;
import com.ruoyi.machine.base.wearing.service.IWearingPartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 易损件管理
 */
@RestController
@RequestMapping("/machine/wearingPart")
@Api(value = "易损件管理模块", tags = "易损件管理接口")
public class WearingPartController extends BaseController {
    @Autowired
    private IWearingPartService wearingPartService;

    /**
     * 获取易损件管理
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取易损件管理列表",notes = "查询所有易损件管理信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody WearingPartCond cond) {
        startPage();
        List<WearingPart> wearingPartList = wearingPartService.selectWearingPartList(cond);
        return getDataTable(wearingPartList);
    }

    /**
     * 导出易损件管理
     *
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody WearingPartCond cond) {
        List<WearingPart> wearingPartList = wearingPartService.selectWearingPartList(cond);
        ExcelUtil<WearingPart> util = new ExcelUtil<>(WearingPart.class);
        return util.exportExcel(wearingPartList, "易损件信息");
    }

    /**
     * 新增易损件管理
     * @param wearingPart
     * @return
     */
    @ApiOperation(value = "新增易损件管理", notes = "新增易损件管理信息")
    @ApiImplicitParam(name = "wearingPart", value = "新增易损件管理", dataType = "WearingPart")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WearingPart wearingPart) {
        WearingPart wearingPart1 = wearingPartService.selectWearingPartByName(wearingPart.getWearingPartName(), wearingPart.getWearingPartSpecification());
        if (StringUtils.isNotNull(wearingPart1)) {
            //存在
            return AjaxResult.error("该规则易损件已存在，请重新添加");
        }
        return toAjax(wearingPartService.insertWearingPart(wearingPart));
    }

    /**
     *
     * 更新易损件管理
     * @param wearingPart
     * @return
     */
    @ApiOperation(value = "修改易损件管理", notes = "修改易损件管理")
    @ApiImplicitParam(name = "wearingPart", value = "修改易损件管理", dataType = "WearingPart")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody WearingPart wearingPart) {
        try {
            wearingPartService.updateWearingPart(wearingPart);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除易损件管理
     * 仅修改删除标志位
     * @param wearingPart
     * @return
     */
    @ApiOperation(value = "删除易损件管理", notes = "删除易损件管理,仅删除标志位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody WearingPart wearingPart) {

        return toAjax(wearingPartService.updateWearingPartDeleteFlag(wearingPart));
    }
}
